package com.bxait.toymail.product.web;

import com.bxait.com.constant.AuthServerConstant;
import com.bxait.com.vo.product.CateLog2Vo;
import com.bxait.toymail.product.entity.CategoryEntity;
import com.bxait.toymail.product.service.CategoryService;
import com.bxait.toymail.product.service.SkuInfoService;
import com.bxait.toymail.product.vo.SkuItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Chen
 * @description: 处理页面跳转的controller
 * @ClassName ViewController
 * @create 2023-03-26 20:58
 * @Version 1.0
 */
@Slf4j
@Controller
public class ViewController{


    @Resource
    @Qualifier("categoryService")
    CategoryService categoryService;

    @Resource
    @Qualifier("skuInfoService")
    SkuInfoService skuInfoService;


    @ResponseBody
    @GetMapping({"/get"})
    public String get(HttpSession session){
        String attribute = (String)session.getAttribute(AuthServerConstant.LOGIN_USER);
        log.error("attribute-------->>>"+attribute);
        return attribute;
    }

//    @ResponseBody
//    @GetMapping({"/set"})
//    public String set(HttpSession session){
//        session.setAttribute("key","666");
//        String key = (String) session.getAttribute("key");
//        return key;
//    }

    /**
     * @param model 存放视图数据
     * @==============History===============<br/>
     * @Description // TODO  处理去首页的请求
     * @Date 2023/3/26 2023/3/26
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [org.springframework.ui.Model]
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        // 查出所以的一级分类
        List<CategoryEntity> categoryEntities =  categoryService.getLevelOneCateGory();
        // 返回首页
        model.addAttribute("category",categoryEntities);
        return "list";
    }

    /**
     * @param
     * @==============History===============<br/>
     * @Description // TODO  封装分类数据进行返回
     * @Date 2023/3/26 2023/3/26
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return []
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<CateLog2Vo>> getCatalogJson(){
        Map<String,List<CateLog2Vo>> catalog = categoryService.getCatalogJson();
        return catalog;
    }

    
    /*
     * @param skuId
     * @param model
     * @==============History===============<br/>
     * @Description //   获取当前商品详情信息
     * @Date 2023/5/4 2023/5/4 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return java.lang.String
     */
    @GetMapping("/{skuId}.html")
    public String skuItem(@PathVariable("skuId") Long skuId, Model model){
        log.info("准备查询{}详情",skuId);
        SkuItemVo vo = skuInfoService.item(skuId);
        model.addAttribute("item",vo);
        return "item";
    }
}
